﻿using Kaskelot.SP.Model.Client;
using Kaskelot.SP.Services.Client.Mapping;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

namespace Kaskelot.SP.Services.Client
{
	[ServiceContract(Namespace = "http://Kaskelot.SP.Services")]
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	public class ClientTodoService
	{
		[OperationContract]
		[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
		public ClientServiceResult<IEnumerable<ClientHandelseLogg>> GetPersonHandelseLoggTodo()
		{
			ClientServiceResult<IEnumerable<ClientHandelseLogg>> csr = new ClientServiceResult<IEnumerable<ClientHandelseLogg>>();

			try
			{
				using (TodoService svc = new TodoService())
				{
					ClientHandelseLogg[] todos = svc.GetPersonHandelseLoggTodo();

					foreach (ClientHandelseLogg cphl in todos)
					{
						cphl.Text = cphl.Text.Length > 65 ? cphl.Text.Substring(0, 60) + " (...)" : cphl.Text;
					}

					csr.Data = todos.ToList().OrderBy(x => x.Datum); ;
				}
			}
			catch (Exception ex)
			{
				csr.ErrorMessage = ex.Message;
				csr.HasError = true;
			}

			return csr;
		}

		[OperationContract]
		[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
		public ClientServiceResult<IEnumerable<ClientHandelseLogg>> GetByggnadHandelseLoggTodo()
		{
			ClientServiceResult<IEnumerable<ClientHandelseLogg>> csr = new ClientServiceResult<IEnumerable<ClientHandelseLogg>>();

			try
			{
				using (TodoService svc = new TodoService())
				{
					ClientHandelseLogg[] todos = svc.GetByggnadHandelseLoggTodo();

					foreach (ClientHandelseLogg cphl in todos)
					{
						cphl.Text = cphl.Text.Length > 65 ? cphl.Text.Substring(0, 60) + " (...)" : cphl.Text;
					}

					csr.Data = todos.ToList().OrderBy(x => x.Datum);
				}
			}
			catch (Exception ex)
			{
				csr.ErrorMessage = ex.Message;
				csr.HasError = true;
			}

			return csr;
		}

		[OperationContract]
		[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
		public ClientServiceResult<IEnumerable<ClientBlankettPost>> GetBlankettPosterTodo(string blankett, bool inkomna, bool pausade, bool godkanda, bool avslagna)
		{
			ClientServiceResult<IEnumerable<ClientBlankettPost>> csr = new ClientServiceResult<IEnumerable<ClientBlankettPost>>();

			try
			{
				using (TodoService svc = new TodoService())
				{
					IEnumerable<BlankettPost> poster = svc.GetBlankettPosterTodo(blankett);

					if (!inkomna) poster = poster.Where(x => x.Status != BlankettPostStatus.Inkommen.ToString());
					if (!pausade) poster = poster.Where(x => x.Status != BlankettPostStatus.Behandlas.ToString());
					if (!godkanda) poster = poster.Where(x => x.Status != BlankettPostStatus.Godkänd.ToString());
					if (!avslagna) poster = poster.Where(x => x.Status != BlankettPostStatus.Nekad.ToString());

					csr.Data = poster.Select(p => p.ToMinimalClientContract());
				}
			}
			catch (Exception ex)
			{
				csr.ErrorMessage = ex.Message;
				csr.HasError = true;
			}

			return csr;
		}
	}
}
